// pages/monitor/components/setting/setting.ts
import {
  getPushSettingApi,
  savePushSettingApi
} from "../../../../api/monitor"
import type { PushData } from "../../../../../typings/types/monitor/index"
Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    daySendTime: '09:30' as string,
    daySendTimeText: "09:30" as string,
    daySendTimeVisible: false as boolean,
    phoneSended: true as boolean,
    minute: '23:59' as string,
    mobileList: [] as string[],
    gzhRemind: false as boolean,
    ifBinding: false as boolean,  //是否绑定微信公众号
    dialogKey: '' as string,
    ifAddMobile: false,
    phoneNumber: '' as string,
    phoneError: false as boolean,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getPushSetting() {
      getPushSettingApi().then(res => {
        this.setData({
          phoneSended: res.content.phoneSended,
          mobileList: res.content.mobileList,
          daySendTime: res.content.daySendTime || "09:30",
          gzhRemind: res.content.wxSended,
          ifBinding: res.content.isBiddingWx
        })
      })
    },
    handleBack() {
      wx.navigateBack()
    },
    showPicker() {
      this.setData({ daySendTimeVisible: true })
    },
    hidePicker() {
      this.setData({
        daySendTimeVisible: false
      })
    },
    onConfirm(e: any) {
      this.setData({ daySendTimeText: e.detail.value })
      this.hidePicker();
    },
    //消息推送
    phoneSendedChange(e: any) {
      this.setData({
        phoneSended: e.detail.value,
      });
      if (!e.detail.value) {
        this.setData({ ifAddMobile: false, phoneNumber: "", phoneError: false })
      }
    },
    //添加手机号
    addMobile() {
      if (!this.data.phoneSended) return wx.showToast({
        title: "请先开启短信推送",
        duration: 2000,
        icon: "none"
      });
      this.setData({ ifAddMobile: true })
    },
    pushMobile() {
      if (!this.data.phoneNumber) return wx.showToast({
        title: "请输入手机号",
        duration: 2000,
        icon: "none"
      });
      if (this.data.phoneError) return wx.showToast({
        title: "手机号格式不正确",
        duration: 2000,
        icon: "none"
      });
      this.data.mobileList.push(this.data.phoneNumber)
      this.setData({ ifAddMobile: false, phoneNumber: "", mobileList: this.data.mobileList })
    },
    onPhoneInput(e: any) {
      const { phoneError } = this.data;
      const isPhoneNumber = /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(e.detail.value);
      if (phoneError === isPhoneNumber) {
        this.setData({
          phoneError: !isPhoneNumber,
          phoneNumber: e.detail.value,
        });
      }

    },
    //删除手机号
    delMobile(e: any) {
      this.data.mobileList.splice(e.target.dataset.delindex, 1)
      this.setData({ mobileList: this.data.mobileList })
    },
    //微信公众号提醒
    gzhRemindChange(e: any) {
      if (!this.data.ifBinding) {
        this.setData({
          gzhRemind: false,
        });
        return wx.showToast({
          title: "请先绑定微信，才可开启公众号提醒",
          duration: 2000,
          icon: "none"
        });
      }
      this.setData({
        gzhRemind: e.detail.value,
      });
    },
    //保存监控设置
    savePushSetting(isHud: boolean = true) {
      this.setData({ ifAddMobile: false, phoneNumber: "", phoneError: false })
      let params: PushData = {
        daySendTime: this.data.daySendTime,
        phoneSended: this.data.phoneSended,
        mobileList: this.data.mobileList,
        emailList: [],
        emailSended: false,
        messageLevelList: ["positive", "tips", "warn", "medium_risk", "high_risk"],
        messageTypeList: ["hearing", "court", "lawsuit", "limit", "trust", "put", "finish", "tax", "industry", "environmental", "violation"],
        popuped: true,
        wxSended: this.data.gzhRemind
      }
      savePushSettingApi(params).then(res => {
        if (res.code === "000" && isHud) {
          wx.showToast({
            title: "监控设置保存成功",
            duration: 2000,
            icon: "none"
          });
        }

      })
    },
    //绑定公众号
    bindFn() {
      let binding = this.data.ifBinding
      wx.navigateTo({
        url: "/pages/monitor/components/gzhBinding/gzhBinding?ifBinding=" + binding
      })
    }
  },
  lifetimes: {
    created() {
    },
    attached() { },
    ready() { },
    moved() { },
    detached() { },
    error() { },
  },
  pageLifetimes: {
    show() {
      this.getPushSetting()
    },
  },
  observers: {

  }
})